#include "addcontrol.h"

#include <QDebug>

AddControl::AddControl(QWidget *parent) : QWidget(parent)
{
    imageWidgetWidth = 320;
    imageWidgetHeight = 240+48;
    maxColumn = 3;
    CX = 0;
    CY = 0;
    this->setFixedSize(imageWidgetWidth, imageWidgetHeight);
    btn = new QPushButton("new", this);
    btn->setFont(QFont("Monospace", 100, QFont::Bold));
    btn->setIcon(QIcon(":/new.jpeg"));
    btn->setIconSize(QSize(320, 70));
    btn->setFixedSize(320,240+48);
    btn->setToolTip("Click to add New video.");

    mainGridLayout = new QGridLayout;
    mainGridLayout->setContentsMargins(0, 0, 0, 0);
    mainGridLayout->setHorizontalSpacing(0);
    mainGridLayout->addWidget(btn, CX, CY, Qt::AlignLeft);

    this->setLayout(mainGridLayout);

    connect(btn, SIGNAL(clicked()), this, SLOT(BtnClicked()));
}

void AddControl::BtnClicked()
{
    publicAddthread();
}

void AddControl::publicAddthread()
{
    int index   = imgWidgets.count();
    CX        = index/maxColumn;  //当前新图像的X坐标
    CY        = index%maxColumn;  //当前新图像的Y坐标
    if(index+1 < maxColumn){
        //当接口小于每行能容纳的最大个数时候
        //增加列宽度
        this->setFixedWidth(imageWidgetWidth*(index+2));
    }
    imgWidgets.append(new ImageWidget(this));
    mainGridLayout->removeWidget(btn);
    mainGridLayout->addWidget(imgWidgets[index],CX,CY);
    if(CY == maxColumn-1){
        //当一行放不下的时候增加行宽度
        //并将按钮放置在下一行
        CX++;
        CY = -1;
        this->setFixedHeight(imageWidgetHeight*(CX+1));
    }
    mainGridLayout->addWidget(btn,CX,CY+1);
}
